package com.lingyuan.product.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.excel.util.MapUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.lingyuan.common.core.domain.R;
import com.lingyuan.common.core.domain.vo.pms.ProductSkuVo;
import com.lingyuan.common.core.exception.ServiceException;
import com.lingyuan.common.core.utils.StringUtils;
import com.lingyuan.common.core.utils.poi.ExcelUtil;
import com.lingyuan.product.domain.SkuEntity;
import com.lingyuan.product.domain.SpuEntity;
import com.lingyuan.product.domain.vo.ItemVo;
import com.lingyuan.product.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * @ClassName SkuController
 * @Description 描述
 * @Author guoxin
 * @Date 2024/9/18 19:17
 */
@RestController
@RequestMapping("/sku")
public class SkuController {
    @Autowired
    private SkuService skuService;
    /**
     * 购物车 拿到商品信息
     */
    @GetMapping("/findBySkuId/{skuId}")
    public R<ProductSkuVo> findBySkuId(@PathVariable("skuId") Long skuId){
       if(null==skuId){
           throw new ServiceException("参数错误");
       }
        ProductSkuVo productSkuVo =  skuService.findBySkuId(skuId);
       return R.ok(productSkuVo);
    }
    /**
     * 商品详细信息
     */
    @GetMapping("/findInfo/{skuId}")
    public R findInfo(@PathVariable("skuId") Long skuId) throws ExecutionException, InterruptedException {
        if (null==skuId){
            throw new ServiceException("参数错误");
        }
        ItemVo itemVo = skuService.findInfo(skuId);
        return R.ok(itemVo);
    }
    /**
     * 导入sku 信息
     */
    @PostMapping("/upload")
    public R  upload(@PathVariable("file") MultipartFile file) throws IOException {
        long l = System.currentTimeMillis();
        EasyExcel.read(file.getInputStream(),SkuEntity.class, new PageReadListener<SkuEntity>(skuEntities -> {
            skuService.bulkAdd(skuEntities);
        })).sheet().doRead();;
        long l1 = System.currentTimeMillis();
        Long elapsedTime = (l1-l) /1000;
        System.out.println("耗时："+elapsedTime+"s");
        return R.ok("导入成功");
    }

    /**
     * 导出sku信息
     * @param
     * @throws IOException
     */
    @GetMapping("/exprot")
    public R export() throws IOException {
        Long l = System.currentTimeMillis();
        skuService.export();//导出
        Long l1 = System.currentTimeMillis();
        Long elapsedTime = (l1-l) /1000;
        System.out.println("耗时："+elapsedTime+"s");
        return R.ok("导出成功");
    }

}
